{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "66627c08",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模板： input_variables=['product_info', 'requires', 'user_question'] input_types={} partial_variables={} template='请你作为产品顾问，根据以下产品信息，回答用户的问题。产品信息：{product_info}， 用户问题：{user_question}要求：{requires}'\n"
     ]
    }
   ],
   "source": [
    "from langchain_core.prompts import  PromptTemplate\n",
    "template_str = \"请你作为产品顾问，根据以下产品信息，回答用户的问题。产品信息：{product_info}， 用户问题：{user_question}要求：{requires}\"\n",
    "template = PromptTemplate.from_template(\n",
    "    template=template_str,\n",
    "    # input_variables=[\"product_info\", \"user_question\", \"requires\"], # 注意注意，这里没有input_variables的变量啦\n",
    ")\n",
    "print(\"模板：\", template)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c9c605cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "path = \"./asset/example1.yaml\"\n",
    "template.save(\"./asset/example1.yaml\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d66d1190",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "load_template:  input_variables=['product_info', 'requires', 'user_question'] input_types={} partial_variables={} template='请你作为产品顾问，根据以下产品信息，回答用户的问题。产品信息：{product_info}， 用户问题：{user_question}要求：{requires}'\n"
     ]
    }
   ],
   "source": [
    "from langchain_core.prompts import load_prompt\n",
    "load_template = load_prompt(path)\n",
    "print(\"load_template: \", load_template)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a48ab509",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'请你作为产品顾问，根据以下产品信息，回答用户的问题。产品信息：XX品牌无线耳机：续航30小时，支持主动降噪，IP54防水，兼容安卓和iOS系统，售价299元， 用户问题：这款耳机能在跑步时用吗？要求：1. 重点说明防水性能是否适合运动场景；2. 用口语化表达，避免专业术语；3. 回答不超过3句话'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 这个时候就是用到了format了\n",
    "prompt_value = load_template.format(\n",
    "    product_info=\"XX品牌无线耳机：续航30小时，支持主动降噪，IP54防水，兼容安卓和iOS系统，售价299元\",\n",
    "    user_question=\"这款耳机能在跑步时用吗？\",\n",
    "    requires=\"1. 重点说明防水性能是否适合运动场景；2. 用口语化表达，避免专业术语；3. 回答不超过3句话\"\n",
    ")\n",
    "prompt_value"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langchain-course",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
